package com.zhiwen.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.zhiwen.common.api.Result;
import com.zhiwen.domain.dto.QuestionVoteDTO;
import com.zhiwen.service.QuestionVoteService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 问题点赞控制器
 */
@Tag(name = "问题点赞", description = "问题点赞相关接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/questions/votes")
public class QuestionVoteController {

    private final QuestionVoteService questionVoteService;

    /**
     * 问题点赞
     *
     * @param voteDTO 点赞数据
     * @return 结果
     */
    @Operation(summary = "问题点赞", description = "对问题进行点赞或者反对，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @SaCheckLogin
    @PostMapping
    public Result<Boolean> voteQuestion(@RequestBody QuestionVoteDTO voteDTO) {
        boolean result = questionVoteService.voteQuestion(voteDTO);
        return Result.success(result);
    }

    /**
     * 取消问题点赞
     *
     * @param questionId 问题ID
     * @return 结果
     */
    @Operation(summary = "取消问题点赞", description = "取消对问题的点赞或反对，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @SaCheckLogin
    @DeleteMapping("/{questionId}")
    public Result<Boolean> cancelVote(@Parameter(description = "问题ID") @PathVariable Long questionId) {
        boolean result = questionVoteService.cancelVote(questionId);
        return Result.success(result);
    }

    /**
     * 检查用户对问题的点赞状态
     *
     * @param questionId 问题ID
     * @return 点赞类型(null-未点赞，1-赞成，2-反对)
     */
    @Operation(summary = "检查点赞状态", description = "检查当前用户对问题的点赞状态，需要登录")
    @SecurityRequirement(name = "Authorization")
    @ApiResponse(responseCode = "200", description = "操作成功")
    @SaCheckLogin
    @GetMapping("/status/{questionId}")
    public Result<Integer> checkVoteStatus(@Parameter(description = "问题ID") @PathVariable Long questionId) {
        Integer voteType = questionVoteService.checkVoteStatus(questionId);
        return Result.success(voteType);
    }
}
